package com.bo.Controller;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.multipart.MultipartFile;
import com.bo.Entity.CompanyInt;
import com.bo.Entity.MyImage;
import com.bo.Entity.SelectModel;
import com.bo.Service.CompanyIntService;
import com.bo.Validator.CompanyIntValidator;
import com.bo.util.ImageUtil;
import com.bo.util.SessionUtil;


@Controller
@RequestMapping(value="/main")
@SessionAttributes("companyInt")
public class CompanyIntController {
	
	@Autowired
	private CompanyIntService companyIntService;
	
	@Autowired
	private CompanyIntValidator companyIntValidator;
	

	@RequestMapping(value="/companyInt.do",method=RequestMethod.GET)
	public String companyInt(Model model,HttpServletRequest request){
		

		CompanyInt companyInt=companyIntService.getCompanyInt(SessionUtil.getCompanyId(request));
		
		if(companyInt==null){
		
		    companyInt=new CompanyInt();
		}
			
		model.addAttribute("companyInt", companyInt);
		
		return "companyInt";
	}
	
	
	
	
	
	@RequestMapping(value="/companyInt.do",method=RequestMethod.POST)
	public String companyInt(@ModelAttribute("companyInt") CompanyInt companyInt
			,BindingResult result,Model model
			,@RequestParam("file1") MultipartFile image1
			,@RequestParam("file2") MultipartFile image2
			,HttpServletRequest request,SessionStatus status){
		
		companyInt.setCompanyId(SessionUtil.getCompanyId(request));
		
		try {
			
			
			String imageName1=ImageUtil.createImageName(image1.getContentType());
			
			companyInt.setImage1(new MyImage(1,imageName1,image1.getSize(),image1.getInputStream()));
		        
			
			switch(companyInt.getModel()){
			   
			   case SelectModel.ONE_IMG_TLTLE_CONTENT : {
				   
				   companyInt.setImage2(new MyImage(2,image2.getInputStream()));

			   };break;
			   
			   case SelectModel.TWO_IMG_TITLE_CONTENT : {
				   
				  String imageName2=ImageUtil.createImageName(image2.getContentType());
				   
			      companyInt.setImage2(new MyImage(2,imageName2,image2.getSize(),image2.getInputStream()));
				   
			   };break;
			   
               default : return "companyInt";
			}
			
			
		    companyIntValidator.validate(companyInt, result);
			
			if(result.hasErrors()){
				
				return "companyInt";
			}

			companyIntService.saveOrUpdate(companyInt);
			
			status.setComplete();
			
		} catch (IOException e) {
			
			e.printStackTrace();
			
		}
		
	   return "redirect:companyInt.do";
	}
	
	
}
